<?php
	/**
	 * 订单商品列表模型
	 */
	 
	 class OrderListModel extends Model{
	 	public $table = 'order_list';
		
		public $validate = array(
			array('goods_num','nonull','商品数量不能为空',2,3),
			array('count_price','nonull','价格不能为空',2,3),
			array('goods_gid','nonull','商品id不能为空',2,3),
			array('order_oid','nonull','订单号不能为空',2,3),
			array('goods_combinestr','商品组合不能为空',2,3),
			array('goods_combine','商品组合不能为空',2,3),
		);
		
		/**
		 * 商品列表写入订单列表中
		 */
		public function writeGoodsList($orderData){
			//将商品明细写入列表列表中
			$orderList = Q('post.orderList'); //要结算的商品
			//获得当前购物车中所有的商品
			$cartData = Cart::getAllData();
			$allGoods = $cartData['goods'];
			p($allGoods);
			//要写入的数据
			$wirteData = array();
			foreach($orderList as $v){
				//重组数组
				if(array_key_exists($v, $allGoods)){
						$writeData['goods_num']= $allGoods[$v]['num'];
						$writeData['count_price'] = $allGoods[$v]['total'];
						$writeData['goods_gid'] = $allGoods[$v]['id'];
						$writeData['order_oid'] = $orderData['orderid'];
						$writeData['goods_combine'] = $allGoods[$v]['options']['combine'];
						//当前商品规格组合的库存
						$num = K('GoodsList')->where("combine='{$writeData['goods_combine']}'")->getField('goods_number');
						if($num<$writeData['goods_num']){
							$this->error = '对不起库存不足请更改数量';
							return false;
						}
						$writeData['goods_combinestr'] = implode(',',$allGoods[$v]['options']['combinestr']);
						if(!$this->create($writeData)) return false;
						$result = $this->add($this->data);
					}		
					if(!$result){
						return false;
					}
			}
			
			return true;
			
		}
		
		
		/**
		 *  返回相应的订单ID的订单列表商品
		 */
		 public function getOrderList($oid){
		 	//将相应的商品图片压入到数据中
			return $this->field('gid,goods_name,goods_listimg,goods_combinestr,goods_num,count_price')
						->join('__goods__ g JOIN __order_list__ ol ON ol.goods_gid=g.gid')
						->where("order_oid={$oid}")
						->all();
		 }
		
			
		
	 }

